<?php
namespace DRY\Module;

/**
 * DonReY Framework 2012 :: Render module - Combines various sources of data to produce a stringified output
 * This module can be used as the "View" part of a MVC pattern, in Push mode ( data + template => output ), but is useful for other purposes as well
 *
 * @author Wavetrex <wavetrex@gmail.com>
 * @link http://www.donrey.net/Documentation/Structure/Modules/render.html
 * @license [GPL v3] http://www.gnu.org/licenses/gpl-3.0.html
 * @version 1.0.dev
 * @package DonReY
 * @subpackage Module.Render
 */
class Render extends base
{
	// Exception/Error codes
	const
		EXC_TRANSFORM_TYPE_NOT_DEFINED = 1
		;

	const
		Autoload_Directory = 'render',
		Autoload_Prefix = 'DRY\Render\Transform',
		Autoload_Suffix = 'transform'
		;

	/**
	 * Creates a render transform object, which can be used to combine input data into output string
	 * @param string $type What kind of transformation engine to use
	 * @param mixed $arguments, $... Data to be sent to the transform's constructor (mostly used for one-shot transforms), which use __toString to return their result
	 *   Example: echo \DRY\core::render()-> factory_Transform('css', array(... css selectors...), 'compact');
	 * @throws Water
	 */
	public function factory_Transform($type)
	{
		// check if type is defined in config
		if(!isset($this-> CFG-> transforms->$type))
			throw new \DRY\Water('render', self::EXC_TRANSFORM_TYPE_NOT_DEFINED, array('type'=>$type));

		$transform_class = self::Autoload_Prefix.'_'.$type;
		$arguments = func_get_args();
		return new $transform_class($this-> CFG-> transforms-> $type, $arguments, $this);
	}
}

namespace DRY\Render;

// might change this to an abstract class instead
interface Transform_base
{
	/**
	 * While it should be possible to send the data to a widget using it's constructor, sometimes it might be necessary to send new data to the widget after it has been instantiated.
	 * This method must be implemented by all transforms to allow this. In combination with __toString() forming the rendering chain.
	 * Transforms could have one or multiple data sources, using func_get_args to retrieve the data elements themselves.
	 */
	public function set_Data();

	/**
	 * All transforms should have toString implemented, so they can be "cast" into a string to generate their output
	 */
	public function __toString();
}